package com.intelligentclass.services;

import com.intelligentclass.entity.Teacher;
import com.intelligentclass.entity.Teaching;
import com.intelligentclass.repositories.TeacherRespository;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherService {

    private TeacherRespository teacherRespository;

    public TeacherService(TeacherRespository teacherRespository) {
        this.teacherRespository = teacherRespository;
    }

    public List<Teaching> getAllTeaching(String tno) {
        return teacherRespository.findAllTeaching(tno);
    }

    public String getTnameByTno(String tno) {
        try {
            String tname = "";
            Teacher t = teacherRespository.getReferenceById(tno);
            tname = t.getTname();
            return tname;
        }catch(EntityNotFoundException e) {
            return null;
        }
    }

    public Teacher searchByTno(String tno) {
        return teacherRespository.findById(tno).orElse(null);
    }

    public Teacher save(Teacher tea){
        return teacherRespository.save(tea);
    }

    public List<Teaching> getTeachingList(String tno){
        Teacher teacher = teacherRespository.findById(tno).orElse(null);
        if(teacher == null){
            return null;
        }
        else {
            return teacher.getTeachingList();
        }
    }

    public String getIntroduce(String tno){
        return teacherRespository.findTeacherIntroduce(tno);
    }
}
